package ytdp.careagle.com.ytdp.app.manager;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
//import com.squareup.picasso.Callback;
//import com.squareup.picasso.Picasso;
//import com.squareup.picasso.Target;
//import com.squareup.picasso.Transformation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.callback.PCallBack;

/**
 * Created by lida on 2017/6/5 0005.
 */

public class ImageLoadManager {
    private static ImageLoadManager manager;

    public synchronized static ImageLoadManager getImageLoadManager() {
        if (manager == null) {
            manager = new ImageLoadManager();
        }
        return manager;
    }

    private ImageLoadManager() {
    }

    public void load(String uri, ImageView imageView) {
        Glide.with(imageView.getContext()).load(uri).into(imageView);
//        Picasso.with(imageView.getContext())
//                .load(uri)
//                .transform(getTransformation(imageView))
//                .config(Bitmap.Config.RGB_565)
//                .into(imageView);
    }

    public void load(String uri, ImageView imageView, boolean original, int imageId) {
        Glide.with(imageView.getContext()).load(uri).error(imageId).into(imageView);

//        Picasso.with(imageView.getContext())
//                .load(uri)
//                .config(Bitmap.Config.RGB_565)
//                .error(imageId)
//                .into(imageView);
    }

    public void loadgif(ImageView imageView, int imageId) {
        Glide.with(imageView.getContext()).load(imageId).asGif().into(imageView);
    }

//    //5.0.1会出现显示不了的问题
//    private static Transformation getTransformation(final ImageView view) {
//        return new Transformation() {
//            @Override
//            public Bitmap transform(Bitmap source) {
//                int targetWidth = view.getWidth();
//
//                //返回原图
//                if (source.getWidth() == 0 || source.getWidth() < targetWidth) {
//                    return source;
//                }
//
//                //如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
//                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
//                int targetHeight = (int) (targetWidth * aspectRatio);
//                if (targetHeight == 0 || targetWidth == 0) {
//                    return source;
//                }
//                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
//                if (result != source) {
//                    // Same bitmap is returned if sizes are the same
//                    source.recycle();
//                }
//                return result;
//            }
//
//            @Override
//            public String key() {
//                return "transformation" + " desiredWidth";
//            }
//        };
//    }

}
